package com.wcs.shortlink.admin.controller;

import com.wcs.shortlink.admin.common.convention.result.Result;
import com.wcs.shortlink.admin.common.convention.result.Results;
import com.wcs.shortlink.admin.dto.req.UserLoginReqDTO;
import com.wcs.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.wcs.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.wcs.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.wcs.shortlink.admin.dto.resp.UserRespDTO;
import com.wcs.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询用户信息
     */
    @GetMapping("/api/shortlink/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
        return Results.success(userService.getUserByUsername(username));

    }

    /*
    查询用户名是否存在
     */
    @GetMapping("/api/shortlink/v1/user/has-username")
    public Result<Boolean> availableUsername(@RequestParam("username") String username) {
        return Results.success(userService.availableUsername(username));
    }


    /**
     * 注册用户
     */
    @PostMapping("/api/shortlink/v1/user/")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){

        userService.register(requestParam);

        return Results.success();
    }

    @PutMapping("/api/shortlink/v1/user/")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){

        userService.update(requestParam);

        return Results.success();
    }

    /*
    用户登录
     */
    @PostMapping("/api/shortlink/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){

        UserLoginRespDTO result = userService.login(requestParam);

        return Results.success(result);
    }

    /*
    检查用户是否登录
     */
    @GetMapping("/api/shortlink/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }


    /*
   用户退出登录
 */
    @DeleteMapping("/api/shortlink/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }


}
